<?php
namespace app\material\controller;
use app\AdminRest;


use app\ApiRest;
use app\card\model\Company;
use app\card\model\Job;
use app\card\model\User;
use app\card\model\UserInfo;
use app\material\model\Brochure;
use app\material\model\BrochureCompany;
use app\material\model\BrochureIm;
use app\material\model\BrochureShare;
use app\material\model\Config;
use app\material\model\File;
use app\material\model\FileCollect;
use app\material\model\FileCompany;
use app\material\model\Folder;
use app\shop\model\AdminCompany;
use app\shop\model\IndexUser;
use app\shop\model\IndexUserInfo;
use think\App;




class IndexBrochure extends ApiRest
{

    protected $model;

    protected $brochure_model;

    protected $file_model;

    protected $folder_model;

    protected $config_model;

    protected $share_model;

    protected $store_id;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new Brochure();

        $this->brochure_model  = new Brochure();

        $this->file_model = new File();

        $this->folder_model  = new Folder();

        $this->config_model = new Config();

        $this->share_model = new BrochureShare();

        $user_model = new UserInfo();

        $this->store_id = $user_model->where(['fans_id'=>$this->getUserId()])->value('company_id');

    }



    /**
     * @author chenniang
     * @DataTime: 2019-12-26 15:40
     * @功能说明:获取公司信息
     */
    public function getCompanyList(){

        $dis[] = ['status' ,'=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $company_model = new AdminCompany();

        $data  = $company_model->companySelectList($dis);

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-07-06 11:28
     * @功能说明:宣传册统计
     */
    public function indexStatistics(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }

        $user_info_model = new IndexUserInfo();

        $is_boss = $this->getUserInfo()['is_boss'];

        if($is_boss==0){

            $company_id = $user_info_model->where(['fans_id'=>$this->getUserId()])->value('company_id');

        }else{

            $company_id = !empty($input['company_id'])?$input['company_id']:0;

        }

        if(!empty($company_id)){

            $dis[] = ['a.company_id','=',$company_id];
        }

        if(!empty($input['b_id'])){

            $dis[] = ['a.b_id','=',$input['b_id']];

            $from['info'] = $this->model->dataInfo(['id'=>$input['b_id']]);

        }
        //分享册数
        $from['share_brochure'] = $this->share_model->shareBrochure($dis,0);
        //分享人数
        $from['share_user']     = $this->share_model->shareUser($dis,0);
        //获客人数
        $from['get_user']       = $this->share_model->shareUser($dis,2);
        //分享次数
        $from['share_times']    = $this->share_model->shareTime($dis,0);

        return $this->success($from);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 13:19
     * @功能说明:
     */
    public function brochureStatisticsList(){

        $input = $this->_param;

        $dis[] = ['b.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','=',1];

       // $dis[] = ['b.type','=',$input['type']];

        if(!empty($input['start_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['b.create_time','between',"$start_time,$end_time"];
        }

        $user_info_model = new IndexUserInfo();

        $is_boss = $this->getUserInfo()['is_boss'];

        if($is_boss==0){

            $company_id = $user_info_model->where(['fans_id'=>$this->getUserId()])->value('company_id');

        }else{

            $company_id = !empty($input['company_id'])?$input['company_id']:0;

        }

        if(!empty($company_id)){

            $dis[] = ['b.company_id','=',$company_id];
        }

        if(!empty($input['b_id'])){

            $dis[] = ['a.b_id','=',$input['b_id']];

            $from['info'] = $this->model->dataInfo(['id'=>$input['b_id']]);

        }

        if($input['type']==0){

            $dis[] = ['b.type','=',0];

            $data = $this->model->shareTimeList($dis);

        }elseif($input['type']==1){

            $dis[] = ['b.type','=',0];

            $data = $this->model->shareUserList($dis);

        }elseif($input['type']==2){

            $dis[] = ['b.type','=',2];

            $data = $this->model->shareUserList($dis);

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 13:38
     * @功能说明:排行榜
     */
    public function staffTopList(){

        $input = $this->_param;

        if(!empty($input['start_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        $user_info_model = new IndexUserInfo();

        $is_boss = $this->getUserInfo()['is_boss'];

        if($is_boss==0){

            $company_id = $user_info_model->where(['fans_id'=>$this->getUserId()])->value('company_id');

        }else{

            $company_id = !empty($input['company_id'])?$input['company_id']:0;

        }

        if(!empty($company_id)){

            $dis[] = ['a.company_id','=',$company_id];
        }


        $dis[] = ['a.b_id','=',$input['b_id']];

        $dis[] = ['a.type','=',$input['type']];

        $share_model = new BrochureShare();

        $data = $share_model->staffTopList($dis,$input['type']);

        return $this->success($data);



    }




    /**
     * @author chenniang
     * @DataTime: 2021-06-25 10:35
     * @功能说明:文件详情
     */
    public function brochureInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        if(!empty($input['staff_id'])){

            $staff['name']   = lbUserName($input['staff_id']);

            $staff['avatar'] = lbUserAvatar($input['staff_id']);

            $staff['company'] = lbUserCompany($input['staff_id']);

            $user_model = new IndexUserInfo();

            $job_model  = new Job();

            $user = $user_model->userInfo(['fans_id'=>$input['staff_id']]);

            if(!empty($user)){

                $staff['mobile'] = $user['phone'];

                $staff['job'] = $job_model->where(['id'=>$user['job_id']])->value('name');

                $staff['job'] = !empty($staff['job'])?$staff['job']:'';
            }


            $data['staff_info'] = $staff;

            $user_info = new UserInfo();

            $company_id = $user_info->where(['fans_id'=>$input['staff_id']])->value('company_id');

            $insert = [

                'uniacid'    => $this->_uniacid,

                'user_id'    => $this->getUserId(),

                'b_id'       => $input['id'],

                'company_id' => $company_id,

                'staff_id'   => $input['staff_id'],

                'type'       => 2

            ];

            $res = $this->share_model->dataAdd($insert);

        }


        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-06-29 15:47
     * @功能说明:宣传册首页列表
     */
    public function brochureList(){

        $input = $this->_param;

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'a.status'  => 1,

        ];

        $map[] = ['a.is_company','=',1];

        $map[] = ['b.company_id','=',$this->store_id];

        $data = $this->model->dataList($dis,10,$map);

        if(!empty($data['data'])){

            $user_info   = new UserInfo();

            $bIm_model   = new BrochureIm();

            $share_model = new BrochureShare();

            $is_boss = $this->getUserInfo()['is_boss'];

            if($is_boss==0){

                $company_id = $user_info->where(['fans_id'=>$this->getUserId()])->value('company_id');

            }else{

                $company_id = !empty($input['company_id'])?$input['company_id']:0;

            }

            foreach ($data['data'] as &$v){

                $dis = [

                    'type' => 2,

                    'brochure_id' => $v['id'],

                    'leader' => 0
                ];

                if(!empty($company_id)){

                    $dis['company_id'] = $company_id;
                }

                $v['watch_num']= $bIm_model->where($dis)->count();


                $where = [

                    'type' => 0,

                    'b_id' => $v['id']
                ];

                if(!empty($company_id)){

                    $where['company_id'] = $company_id;
                }

                $v['share_times'] = $share_model->where($where)->count();

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-12 11:09
     * @功能说明:分享画册
     */
    public function shareBrochure(){

        $input = $this->_input;

        $user_info = new UserInfo();
        //说明是员工
        if(empty($input['staff_id'])){

            $company_id = $user_info->where(['fans_id'=>$this->getUserId()])->value('company_id');

        }else{

            $company_id = $user_info->where(['fans_id'=>$input['staff_id']])->value('company_id');

        }

        $insert = [

            'uniacid'    => $this->_uniacid,

            'user_id'    => $this->getUserId(),

            'b_id'       => $input['id'],

            'company_id' => $company_id,

            'staff_id'   => empty($input['staff_id'])?$this->getUserId():$input['staff_id']
        ];

        $res = $this->share_model->dataAdd($insert);

        return $this->success($res);

    }



//    public function












}
